package com.app.xinfadi.widget.image_browser;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.app.xinfadi.R;
import com.app.xinfadi.base.MyApplication;
import com.flyco.animation.BaseAnimatorSet;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.List;


public class ImageBrowserPopupWindow extends PopupWindow {
	private View drop_downlist_popupwindow;
	private Context context;
	public ListView image_browser_list;
	private ImageBrowserAdapter imageBrowserAdapter;
	private List<ImageFloder> mDatas;
	private String TAG = "ImageBrowserPopupWindow";

	public ImageBrowserPopupWindow(Context context, int width, int height, List<ImageFloder> datas) {
		this.context = context;
		this.mDatas = datas;
		drop_downlist_popupwindow = LayoutInflater.from(context).inflate(R.layout.image_browser_pop_list, null);
		image_browser_list = (ListView) drop_downlist_popupwindow.findViewById(R.id.image_browser_list);
		this.setContentView(drop_downlist_popupwindow);
		this.setWidth(width);
		this.setHeight(height);
//		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// this.update();
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
		imageBrowserAdapter = new ImageBrowserAdapter(context, mDatas);
		image_browser_list.setAdapter(imageBrowserAdapter);
		image_browser_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mImageDirSelected.selected(mDatas.get(position), position);
			}
		});
	}

	public ImageBrowserPopupWindow(Context context, int width, int height) {
		this.context = context;
		drop_downlist_popupwindow = LayoutInflater.from(this.context).inflate(R.layout.image_browser_pop_list, null);
		image_browser_list = (ListView) drop_downlist_popupwindow.findViewById(R.id.image_browser_list);
		this.setContentView(drop_downlist_popupwindow);
		this.setWidth(width);
		this.setHeight(height);
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// this.update();
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
	}

	@SuppressLint("NewApi")
	@Override
	public void dismiss() {
		// ObjectAnimator animator1 = ObjectAnimator.ofFloat(getContentView(),
		// "translateY", 0f, -1300f);// 添加了addUpdateListener
		// // 方法
		// // ko
		// // 随意取名
		// animator1.setDuration(1200).start();
		// animator1.addUpdateListener(new AnimatorUpdateListener() {
		// @Override
		// public void onAnimationUpdate(ValueAnimator arg0) {
		// float f = (Float) arg0.getAnimatedValue();
		// getContentView().setY(f); // 通过设置view的Y轴
		// // 实现移动
		// }
		// });
		windowOut();
		super.dismiss();
	}

	public ImageBrowserPopupWindow(Context context, List<ImageFloder> datas) {
		this(context, 0, 0, datas);
	}

	public ImageBrowserPopupWindow(Context context) {
		this(context, 0, 0, null);
	}

	public void setAdapter(ListAdapter adapter) {
		image_browser_list.setAdapter(adapter);
	}

	public ListView getImage_browser_list() {
		return image_browser_list;
	}

	public void setImage_browser_list(ListView image_browser_list) {
		this.image_browser_list = image_browser_list;
	}

	/**
	 * Image 文件夹选择
	 * 
	 * @Function: TODO <br/>
	 * @date 2015-2-9 上午10:46:36 <br/>
	 * @author WuTengFei <br/>
	 * 
	 */
	public interface OnImageDirSelected {
		void selected(ImageFloder floder, int position);
	}

	private OnImageDirSelected mImageDirSelected;

	public void setOnImageDirSelected(OnImageDirSelected mImageDirSelected) {
		this.mImageDirSelected = mImageDirSelected;
	}
	public void windowOut(){
		BaseAnimatorSet windowOutAs = new BaseAnimatorSet() {
			@Override
			public void setAnimation(View view) {
				com.nineoldandroids.animation.ObjectAnimator rotationX = com.nineoldandroids.animation.ObjectAnimator.ofFloat(view, "rotationX", 10, 0f).setDuration(150);
				rotationX.setStartDelay(200);
				animatorSet.playTogether(
						com.nineoldandroids.animation.ObjectAnimator.ofFloat(view, "scaleX", 0.8f, 1.0f).setDuration(350),
						com.nineoldandroids.animation.ObjectAnimator.ofFloat(view, "scaleY", 0.8f, 1.0f).setDuration(350),
//                    ObjectAnimator.ofFloat(view, "alpha", 1.0f, 0.5f).setDuration(350),
						com.nineoldandroids.animation.ObjectAnimator.ofFloat(view, "rotationX", 0f, 10).setDuration(200),
						rotationX,
						com.nineoldandroids.animation.ObjectAnimator.ofFloat(view, "translationY", -0.1f * MyApplication.getInstance().screenHeight, 0).setDuration(350)
				);
			}
		};
		windowOutAs.duration(350).playOn(drop_downlist_popupwindow);
	}
	public class ImageBrowserAdapter extends BaseAdapter {

		private Context context;
		private List<ImageFloder> datas;
		private DisplayImageOptions options;
		private ImageSize mImageSize;

		public ImageBrowserAdapter(Context context, List<ImageFloder> datas) {
			this.context = context;
			this.datas = datas;
			mImageSize = new ImageSize(250, 250);
			options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.image_failed)
					.cacheOnDisk(true).bitmapConfig(Bitmap.Config.ARGB_4444).build();
		}

		@Override
		public int getCount() {
			return datas.size();
		}

		@Override
		public Object getItem(int arg0) {
			return datas.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.image_browser_pop_list_item, null);
				holder = new ViewHolder();
				holder.id_dir_item_count = (TextView) convertView.findViewById(R.id.id_dir_item_count);
				holder.id_dir_item_image = (ImageView) convertView.findViewById(R.id.id_dir_item_image);
				holder.id_dir_item_name = (TextView) convertView.findViewById(R.id.id_dir_item_name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.id_dir_item_name.setText(datas.get(position).getName());
			final ImageView iamgeview = holder.id_dir_item_image;
			ImageLoader.getInstance().loadImage("file://" + datas.get(position).getFirstImagePath(), mImageSize,
					options, new SimpleImageLoadingListener() {
						@Override
						public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
							super.onLoadingComplete(imageUri, view, loadedImage);
							iamgeview.setImageBitmap(loadedImage);
						}
					});
			holder.id_dir_item_count.setText(datas.get(position).getCount() + "张");
			return convertView;
		}

		class ViewHolder {
			ImageView id_dir_item_image;
			TextView id_dir_item_name;
			TextView id_dir_item_count;
		}

	}

}
